package com.dhcc.consumer.listener.lazyQueueListener;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

@Component
public class MessageLazyQueueListener01 {

	/**
	 * 与死信队列不同点two，不能给正常的队列绑定消费者
	 * @param message
	 * @param channel
	 */
	@RabbitListener(queues = "lazy-letter-queue")
	public void listenerSimple(Message message, Channel channel) {
		// message分装了消息
		byte[] body = message.getBody();
		String content = new String(body);
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		try {

			System.out.println("MessageLazyQueueListener01...msg ----->" + content);

			// 给RabbitMQ服务端返回ack
			channel.basicAck(deliveryTag, true);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				// 响应unack
				channel.basicNack(deliveryTag, true, true);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
